package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 99037
 */
@RestController
@RequestMapping({"/food"})
public class FoodController {

    @Reference
    private FoodService foodService;

    @RequestMapping({"/downLoad"})
    public Result downLoad(HttpServletResponse response, String filePath, HttpServletRequest request)
            throws Exception {
        String path = request.getSession().getServletContext().getRealPath("template");
        String fileName = new String("food.xls".getBytes(), "iso-8859-1");

        response.setHeader("ContentType", request.getSession().getServletContext().getMimeType("food.xls"));

        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);

        File file = new File(path, fileName);

        InputStream input = new FileInputStream(file);

        OutputStream out = response.getOutputStream();


        byte[] buff = new byte[1024];

        int index = 0;


        while ((index = input.read(buff)) != -1) {

            out.write(buff, 0, index);

            out.flush();
        }
        out.close();
        input.close();
        return null;
    }

    @RequestMapping({"/upload"})
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        System.out.println(excelFile);
        try {
            List<String[]> excel = POIUtils.readExcel(excelFile);
            List<Food> list = new ArrayList();

            for (String[] foods : excel) {
                Food food=new Food();

                String id = foods[0];
                food.setId(Integer.parseInt(id));

                String name = foods[1];
                food.setName(name);

                String tip = foods[2];
                food.setTip(tip);

                String unit = foods[3];
                food.setUnit(unit);

                food.setAge("0-100");

                list.add(food);
            }

            foodService.add(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "批量导入食品数据失败,请检查是否重复导入");
        }

        return new Result(true, "批量导入食品数据成功");
    }

    @RequestMapping({"/findByPage"})
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {

        System.out.println(queryPageBean);

        Integer pageSize = queryPageBean.getPageSize();
        System.out.println(pageSize);

        System.out.println(queryPageBean.toString());

        PageResult list = foodService.findAll(queryPageBean);
        return new PageResult(list.getTotal(), list.getRows());
    }

    @RequestMapping({"/add"})
    public Result add(@RequestBody Food food) {
        System.out.println(food);
        try {
            foodService.addFood(food);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增检查项失败");
        }
        return new Result(true, "新增检查项成功");
    }

    @RequestMapping({"/findAll"})
    public Result findAll(Integer id) {
        try {
           List<Food> list= foodService.findAllFoods(id);
            return new Result(true, "查询成功成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败失败");
        }
    }

    @RequestMapping("/findFoodIdsByOrderId")
    public Result findFoodIdsByOrderId(Integer id) {
        try {
           List<Integer> list= foodService.findFoodIdsByOrderId(id);
            return new Result(true, "查询成功成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败失败");
        }
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            foodService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Food food){
        try{

            foodService.edit(food);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }




}

